﻿using System;
using System.Windows.Media;
using System.Windows.Threading;
using CI.Misc;
using CI.Organism;
using FormsColor = System.Drawing.Color;
using WPFColor = System.Windows.Media.Color;


namespace CI.PW.ViewModels
{
    public class PixelViewerViewModel : DataModel
    {
        private SolidColorBrush _preColour;
        public SolidColorBrush PreColour
        {
            get { return _preColour; }
            private set
            {
                if (value == _preColour)
                    return;

                value.Freeze();
                _preColour = value;
                RaisePropertyChanged("PreColour");
            }
        }

        private SolidColorBrush _postColour;
        public SolidColorBrush PostColour
        {
            get { return _postColour; }
            private set
            {
                if (value == _postColour)
                    return;

                value.Freeze();
                _postColour = value;
                RaisePropertyChanged("PostColour");
            }
        }

        public PixelViewerViewModel(VisualSynapseIO visualSynapseIO)
        {
            visualSynapseIO.VS.RegisterCooridanteListerner((int)visualSynapseIO.XOffset, (int)visualSynapseIO.YOffset, UpdateDisplay);
            PreColour = new SolidColorBrush(Colors.Black);
            PostColour = PreColour;
        }

        private void UpdateDisplay(FormsColor inputValue, long outputvalue)
        {
            if (CompareColours(PreColour, inputValue))
            {
                UIDispatcher.BeginInvoke(DispatcherPriority.Background, (Action)
                                                                        delegate
                                                                        {
                                                                            PreColour =
                                                                                new SolidColorBrush(Color.FromArgb(
                                                                                            inputValue.A,
                                                                                            inputValue.R,
                                                                                            inputValue.G,
                                                                                            inputValue.B));
                                                                        });
            }

            //var pColour = FormsColor.FromArgb((int) outputvalue);
            //if (CompareColours(PostColour, pColour))
            //{
            //    UIDispatcher.BeginInvoke(DispatcherPriority.Background, (Action)
            //                                                            delegate
            //                                                            {
            //                                                                PostColour =
            //                                                                    new SolidColorBrush(Color.FromArgb(
            //                                                                                pColour.A,
            //                                                                                pColour.R,
            //                                                                                pColour.G,
            //                                                                                pColour.B));
            //                                                            });
            //}

        }

        private static bool CompareColours(SolidColorBrush screenColor, FormsColor inputValue)
        {
            return screenColor.Color.A != inputValue.A || screenColor.Color.B != inputValue.B
                   || screenColor.Color.G != inputValue.G || screenColor.Color.R != inputValue.R;
        }
    }
}
